<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" width="80%" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false">
        <el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right" style="height:700px;overflow:auto">
            <div class="title">报修信息</div>
           <el-row :gutter="1">
                <el-col :span="6">
                    <el-form-item label="设备代码" prop="code">
                        <el-input v-model="detail.code" placeholder="设备代码" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="设备名称" prop="title">
                        <el-input v-model="detail.title" placeholder="设备名称" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="设备类别" prop="class_title">
                        <el-input v-model="detail.class_title" placeholder="设备类别" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="故障类别" prop="fault_title">
                        <el-input v-model="detail.fault_title" placeholder="故障类别" readonly></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="6">
                    <el-form-item label="紧急程度" prop="urgent_name">
                        <el-input v-model="detail.urgent_name" placeholder="紧急程度" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="报修人" prop="maintenance_admin_name">
                        <el-input v-model="detail.maintenance_admin_name" placeholder="报修人" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="报修时间" prop="maintenance_create_time">
                        <el-date-picker v-model="detail.maintenance_create_time" :value-format="'YYYY-MM-DD'"  style="width:100%" type="date" disabled />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="故障描述" prop="remark">
                        <el-input v-model="detail.remark" placeholder="故障描述" readonly></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="6">
                    <el-form-item label="响应人" prop="work_admin_name">
                        <el-input v-model="detail.work_admin_name" placeholder="响应人" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="响应时间" prop="work_create_time">
                        <el-date-picker v-model="detail.work_create_time" :value-format="'YYYY-MM-DD'"  style="width:100%" type="date" disabled />
                    </el-form-item>
                </el-col>
            </el-row>
            <div class="title">维修汇报信息</div>
            <el-row :gutter="1">
                <el-col :span="6">
                    <el-form-item label="维修人" prop="user_name">
                        <el-input v-model="detail.user_name" placeholder="维修人" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="维修开始时间" prop="start_date">
                        <el-date-picker v-model="detail.start_date" type="datetime" placeholder="开始时间" format="YYYY-MM-DD HH:mm:ss" date-format="MMM DD, YYYY" time-format="HH:mm" style="width:100%" disabled />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="维修结束时间" prop="end_date">
                        <el-date-picker v-model="detail.end_date" type="datetime" placeholder="结束时间" format="YYYY-MM-DD HH:mm:ss" date-format="MMM DD, YYYY" time-format="HH:mm" style="width:100%" disabled />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="协作人" prop="assist_id">
                        <el-select v-model="detail.assist_id"  filterable style="width: 100%" placeholder="请选择协作人" clearable multiple disabled>
                            <el-option v-for="item in humList" :key="item.id" :label="item.name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="6">
                    <el-form-item label="故障原因" prop="task_reason">
                        <el-input v-model="detail.task_reason" placeholder="故障原因" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="维修内容" prop="task_handle">
                        <el-input v-model="detail.task_handle" placeholder="维修内容" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="验证人" prop="check_admin">
                        <el-select v-model="detail.check_admin" style="width: 100%" placeholder="验证人" disabled>
                            <el-option v-for="item in humList" :key="item.id" :label="item.name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="验证时间" prop="check_time">
                        <el-input v-model="detail.check_time" placeholder="验证时间" disabled></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="备注" prop="task_remark">
                <el-input v-model="detail.task_remark" type="textarea" clearable readonly></el-input>
            </el-form-item>
            <el-row :gutter="1">
                <el-col :span="6">
                    <el-form-item label="故障图片" prop="img">
                        <sc-upload v-model="detail.img" draggable disabled></sc-upload>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="维修图片" prop="task_img">
                        <sc-upload v-model="detail.task_img" draggable disabled></sc-upload>
                    </el-form-item>
                </el-col>
            </el-row>

            <div class="title">维修评价</div>
            <el-row :gutter="1">
                <el-col :span="6">
                    <el-form-item label="产生原因" prop="new_task_reason">
                        <el-input v-model="form.new_task_reason" clearable placeholder="产生原因"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="解决办法" prop="new_task_handle">
                        <el-input v-model="form.new_task_handle" clearable placeholder="解决办法"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="优化建议" prop="new_suggest">
                        <el-input v-model="form.new_suggest" clearable placeholder="优化建议"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="备注" prop="new_task_remark">
                        <el-input v-model="form.new_task_remark" clearable placeholder="请输入备注"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="24">
                <el-form-item label="记录流程" prop="check_status">
                    <el-steps style="width: 100%;" :active="active" align-center finish-status="success">
                        <el-step v-for="(line, key) in lineLists" :key="key" :title="line.title">
                            <template #description>
                                <p v-for="(item, key) in line.items" :key="key">{{ setStepDesc(item) }}</p>
                            </template>
                        </el-step>
                    </el-steps>
                </el-form-item>
            </el-col>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
        this.lineLists = [
            { title: "故障报修", items: ["maintenance_admin_name", "create_time"]},
            { title: "故障审核", items: ["record_name", "check_time"] },
           { title: "故障派工", items: ["work_admin_name", "work_create_time"]},
            { title: "维修开始", items: ["user_name", "start_date"] },
            { title: "维修结束", items: ["user_name", "end_date"] },
            { title: "维修验证", items: ["check_admin_name", "check_time"] }
        ];
		return {
			mode: "add",
			titleMap: {
				add: '新增保养记录',
				edit: '修改评价',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				id: "",
				new_task_reason: "",
				new_task_handle: "",
                new_suggest:"",
                new_task_remark:"",
			},
            detail:{
                code:"",
                title:"",
                class_title:"",
                fault_title:"",
                urgent_name:"",
                user_neme:"",
                create_time:"",
                admin_name:"",
                user_name:"",
                work_admin_name:"",
                work_create_time:"",
                maintenance_admin_name:"",
                maintenance_create_time:"",

                start_date:"",
                end_date:"",
                task_remark:"",
                task_reason:"",
                task_handle:"",
                check_admin:"",
                check_time:"",
                task_img:"",
                img:"",
            },
            // 流程数据字段
            step: {
                maintenance_admin_name: "",
                work_admin_name: "",
                check_admin: "",
                user_name: "",
                record_name:"",
                check_admin_name:"",
                create_time: "",
                check_time: "",
                start_date: "",
                end_date: "",
                work_create_time: ""
            },
            humList:[]
		}
	},
	mounted () {
        this.getHum();
    },
    computed: {
        active() {
            return this.lineLists.length + 1;
        }
    },
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单注入数据
		async setData(data) {
            var res = await this.$API.device.deviceTask.detail.post({id:data.id});
            console.log(res);
            if(res.data.assist_id){
                let a  = (res.data.assist_id).split(",")
                let b = []
                a.forEach(item => {
                    b.push(Number(item))
                });
                this.detail.assist_id = b
            }
			this.form.id = data.id
            this.form.new_task_reason = res.data.new_task_reason
            this.form.new_task_handle = res.data.new_task_handle
            this.form.new_suggest = res.data.new_suggest
            this.form.new_task_remark = res.data.new_task_remark

            this.detail.code = res.data.device_code
            this.detail.title = res.data.device_title
            this.detail.class_title = res.data.class_title
            this.detail.fault_title = res.data.fault_title
            this.detail.urgent_name = res.data.urgent_name
            this.detail.create_time = res.data.create_time
            this.detail.user_name = res.data.user_name
            this.detail.admin_name = res.data.admin_name
            this.detail.work_admin_name = res.data.work_admin_name
            this.detail.work_create_time = res.data.work_create_time
            this.detail.maintenance_admin_name = res.data.maintenance_admin_name
            this.detail.maintenance_create_time = res.data.maintenance_create_time
            this.detail.img = res.data.img
            this.detail.start_date = res.data.start_date
            this.detail.end_date = res.data.end_date
            this.detail.remark = res.data.remark
            this.detail.task_remark = res.data.task_remark
            this.detail.img = res.data.img
            this.detail.task_img = res.data.task_img
            this.detail.check_admin = Number(res.data.check_admin)
            this.detail.check_time = res.data.check_time
            this.detail.task_handle = res.data.task_handle
            this.detail.task_reason = res.data.task_reason
            this.setStepData(res.data);
		},
        setStepData(data) {
            this.injectData(this.step, data);
        },
        // 注入特定数据
        injectData(metaData, data) {
            for (const key in metaData) {
                if (Object.hasOwnProperty.call(metaData, key)) {
                    const element = data[key];
                    metaData[key] = element;
                }
            }
        },
        setStepDesc(prop) {
            return this.step?.[prop] ?? "";
        },
        async getHum(){
            var res = await this.$API.all.getAdmin.get({is_page:0});
            this.humList = res.data
        },
        //表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
                    res = await this.$API.device.deviceTask.evaluate.post(this.form);
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
	}
}
</script>

<style></style>
